﻿Imports System.Data.OleDb
Public Class QuestionAnswers
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("booksConn").ConnectionString)

        Dim quest As String = "Select Question from Questions where ID=@f1"
        Dim questcmd As OleDbCommand = New OleDbCommand(quest, oleDbConn)
        questcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        oleDbConn.Open()
        Dim dr As OleDbDataReader = questcmd.ExecuteReader()

        dr.Read()
        Dim question = dr.GetValue(0)
        Tb_question.Text = question.ToString

        oleDbConn.Close()

    End Sub

    Protected Sub Btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_submit.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("booksConn").ConnectionString)

        Dim hasAnswered As String = "Select * from QuestionAnswer where QuestFkId=@f1 and UserFkId=@f2"
        Dim hasAnsweredcmd As OleDbCommand = New OleDbCommand(hasAnswered, oleDbConn)
        hasAnsweredcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        hasAnsweredcmd.Parameters.AddWithValue("@f2", User.Identity.Name)

        oleDbConn.Open()
        Dim dr As OleDbDataReader = hasAnsweredcmd.ExecuteReader()
        Dim sql As String
        If dr.HasRows() Then
            sql = "Update QuestionAnswer SET Answers=@f3 and CreatedDate=@f4 where QuestFkId=@f1 and UserFkId=@f2 "
        Else

            sql = "INSERT INTO QuestionAnswer(QuestFkId,UserFkId,Answers,CreatedDate) VALUES (@f1,@f2,@f3,@f4)"
        End If
        Dim cmd As OleDbCommand = New OleDbCommand(sql, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f3", Tb_qa.Text)
        cmd.Parameters.AddWithValue("@f4", Convert.ToString(DateAndTime.Now))
        cmd.ExecuteNonQuery()
        oleDbConn.Close()
        Response.Redirect("QuestionAnswers.aspx?ID=" & Request.Params("ID"))



    End Sub
End Class